package cc.fs.study.smart_app;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * apk 帮助类
 */
public class ApkUtils {

    /**
     * 获取安装包位置
     * 如：/data/app/app.apk
     */
    public static String getSourceApkPath(Context context){
        try {
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(getPackageName(context), 0);
            return appInfo.sourceDir;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取包名
     */
    public static String getPackageName(Context context){
        try {
            PackageInfo info;
            PackageManager packageManager = context.getPackageManager();
            info = packageManager.getPackageInfo(context.getPackageName(), 0);
            return info.packageName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取当前版本
     */
    public static String getVision(Context context){
        try {
            PackageInfo info;
            PackageManager packageManager = context.getPackageManager();
            info = packageManager.getPackageInfo(context.getPackageName(), 0);
            return info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "1.0";
        }
    }

}
